package hawmeterclient;

import java.util.Date;

import hawmeterclient.HAWMeterClientServer.Mode;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public class HAWMeterClient {
	boolean counter=true;
	
	/**
	 * Erzeugt einen Wert der auf die vier anzeigen gesetzt wird
	 */
	@WebMethod
	public void setValue(){
		HAWMeterClientServer.warten.release();
		int val =(int) (new Date().getTime() % 20000) /100; //%200
		  if(val>100){
		    val=200-val;
		  }
		for(int i=0; i <=3; i++){
			if(HAWMeterClientServer.metering[i]==null)continue;
			HAWMeterClientServer.metering[i].setValue(val);
		}
		
	}
	
	/**
	 * @retrun URL des Koordinators
	 */
	
	@WebMethod
	public String getKoor() {
		if(HAWMeterClientServer.mode==Mode.KOORDINATOR){
			return HAWMeterClientServer.clientUrl;
		}else{
			return HAWMeterClientServer.koor;
		}
		
	}
	
	/**
	 * @retrun URL der Anzeigen (HAWMetering)
	 */
	@WebMethod
	public String getHawurl() {
		return HAWMeterClientServer.hawurl;
	}
	
	/**
	 * 
	 * @return false immmer :-)
	 */
	@WebMethod
	public Boolean trigger( @WebParam(name = "liste1") String list1, 
							@WebParam(name = "liste2") String list2,
							@WebParam(name = "liste3") String list3,
							@WebParam(name = "liste4") String list4,
							@WebParam(name = "hawurl") String hawurl){
		HAWMeterClientServer.list[0]=list1;
		HAWMeterClientServer.list[1]=list2;
		HAWMeterClientServer.list[2]=list3;
		HAWMeterClientServer.list[3]=list4;
		HAWMeterClientServer.hawurl=hawurl;
		
		setValue();
		
		return false;
	}
	
	/**
	 * zum anmelden an einer der 4 Anzeigen
	 * @return false falls die Anzeige bereits von einem anderen Sensor belegt ist
	 */
	@WebMethod
	public Boolean anmelden(
			@WebParam(name = "string") String is,
			@WebParam(name = "url") String url) {
		if(is.equals("nw") && HAWMeterClientServer.list[0].equals("")){
			HAWMeterClientServer.list[0]=url;
			return true;
		} else if(is.equals("no") && HAWMeterClientServer.list[1].equals("")){ 
			HAWMeterClientServer.list[1]=url;
			return true;
		} else if(is.equals("sw") && HAWMeterClientServer.list[2].equals("")){ 
			HAWMeterClientServer.list[2]=url;
			return true;
		} else if(is.equals("so") && HAWMeterClientServer.list[3].equals("")){ 
			HAWMeterClientServer.list[3]=url;
			return true;
		} 
		return false;
	}
}
